///////////////////////////////page_carManger//////////////////////////
$(document).on("pageInit", "#page_carManger", function(e, pageId, $page) {
    my.givetitle('车辆管理');
                setTimeout(function(){
                 $.showPreloader()
                },1)

      	        var key=my.get_key(my.getCookie('token'))
      	        var info_carManger={
      						//'u_userid':sessionStorage.id,
      						'u_userid':my.getCookie('id'),
      					}
    				info_carManger=JSON.stringify(info_carManger)
                    info_carManger=my.cryptoASEencrypt(info_carManger,key)
      	            $.ajax({
      						type:"post",
      						url:api.usercarlist,
      						data:{
      							'api_value':info_carManger
      						},
      						async:true,
      						error:function(){
      							$.hidePreloader()
      							$.alert("请求失败，请刷新重试");
      						},
      						success:function(data){

      							if(data.match("err_code")!=null){
      								$.hidePreloader()
      								switch(JSON.parse(data).err_code)
      								{
      									case 40003:
      									    my.tokenExpire()
      									   break;
      									default:
      									   $.alert(JSON.parse(data).err_msg);

      								}

      							}
      							else{
      								      data=JSON.parse(data)
                                          data=JSON.parse(my.cryptoASEdencrypt(data.data,key))
                                          console.log(data)
      								    //循环输出车辆
      								     $('.CM-list').empty()
      								    for(i=0;i<data.length;i++){
      								     var none='display:none;';
      								     var none_in='display:none'
      								     var none_edit='display:none'
      								     var state_doc='未认证'
      								     if(data[i].auditresult_state==1){
      								     	none_in='display:inline-block;'
      								     	state_doc='认证通过'
      								     }
      								     if(data[i].auditresult_state==0){
      								     	state_doc='认证中'
      								     }
      								     if(data[i].auditresult_state==2){
      								     	state_doc='认证失败：'+data[i].resultreason
      								     }

      								      var list='<li data-state="'+data[i].auditresult_state+'" name="'+ data[i].usercar_id +'"><div class="car-edit"><div class="carManger-list-top"><div><span><img src="/h5car/img/icon/qcc.png" ></span><p class="brand_chinese_name">'
      					                                      + data[i].brand_chinese_name+'-'+data[i].cars_name +'</p><div class="clear"></div></div><div class="car_plate">'
      					                                      + data[i].car_plate +'</div><div class="auditresult_state"><img src="/h5car/img/icon/ok.png" style="width:15px;margin: 0 5px -2px 0;'+ none_in +'">'
      					                                      + state_doc +'</div></div></div><div class="carManger-list-btn" style="'+ none +'"><div><a href='+carRentalUrl+' class="external"><img src="/h5car/img/icon/rll.png">出租</a></div><div class="clear"></div></div><div class="carManger-list-btn carManger-list-btn-edit" style="'+ none_edit +'"><div class="r-check"><img src="/h5car/img/icon/bj.png">重新认证</div><div class="clear"></div></div></li>'
      								      $('.CM-list').append(list)
      								    }
      							          }
      							        //汽车编辑

      								     	$('.car-edit').click(function(){

      								     			 sessionStorage.car_id=$(this).closest('li').attr('name')
                                                     $.router.load(carEditUrl,true);


      							        })


      							        $('.carManger-list-btn-edit').click(function(){
      							        	sessionStorage.car_carid=$(this).closest('li').attr('name')
      							        	//$.router.loadPage('RecheckCar.html')
                                            $.router.load(RecheckCarUrl,true);
      							        })
      							        $.hidePreloader()
      			                    },
      		                 });
      		    //下拉刷新start
      		    $(document).on('refresh', '.pull-to-refresh-content-carMangerCarlist',function() {
      		    	var info_carManger_refresh={
      						//'u_userid':sessionStorage.id,
      						'u_userid':my.getCookie('id'),
      					}
    				info_carManger_refresh=JSON.stringify(info_carManger_refresh)
                    info_carManger_refresh=my.cryptoASEencrypt(info_carManger_refresh,key)
      		    	$.ajax({
      						type:"post",
      						url:api.usercarlist,
      						data:{
      							'api_value':info_carManger_refresh
      						},
      						async:true,
      						error:function(){
      							$.pullToRefreshDone('.pull-to-refresh-content');
      							$.toast("请求失败，请刷新重试");
      						},
      						success:function(data){
      							if(data.match("err_code")!=null){
      								$.pullToRefreshDone('.pull-to-refresh-content');
      								switch(JSON.parse(data).err_code)
      								{
      									case 40003:
      									    my.tokenExpire()
      									   break;
      									default:
      									   $.toast(JSON.parse(data).err_msg);

      								}

      							}else{
      								    //sessionStorage.carManger_list=my.cryptoASEdencrypt(data,key)
      								   // data=JSON.parse(my.cryptoASEdencrypt(data,key))
      								      data=JSON.parse(data)
                                          data=JSON.parse(my.cryptoASEdencrypt(data.data,key))

      								    //循环输出车辆
      								    $('.CM-list').empty()
      								    for(i=0;i<data.length;i++){
      								     var none='display:none;';
      								     var none_in='display:none'
      								     var none_edit='display:none'
      								     var state_doc='未认证'
      								     if(data[i].auditresult_state==1){
      								     	none_in='display:inline-block;'
      								     	state_doc='认证通过'
      								     }
      								     if(data[i].auditresult_state==0){
      								     	state_doc='认证中'
      								     }
      								     if(data[i].auditresult_state==2){
      								     	state_doc='认证失败：'+data[i].resultreason
      								     }
      								      var list='<li data-state="'+data[i].auditresult_state+'"  name="'+ data[i].usercar_id +'"><div class="car-edit"><div class="carManger-list-top"><div><span><img src="/h5car/img/icon/qcc.png" ></span><p class="brand_chinese_name">'
      					                                      + data[i].brand_chinese_name +'-'+data[i].cars_name +'</p><div class="clear"></div></div><div class="car_plate">'
      					                                      + data[i].car_plate +'</div><div class="auditresult_state"><img src="/h5car/img/icon/ok.png" style="width:15px;margin: 0 5px -2px 0;'+ none_in +'">'
      					                                      + state_doc +'</div></div></div><div class="carManger-list-btn" style="'+ none +'"><div><img src="/h5car/img/icon/rll.png">出租</div><div class="clear"></div></div><div class="carManger-list-btn carManger-list-btn-edit" style="'+ none_edit +'"><div class="r-check open-popup"><img src="/h5car/img/icon/bj.png">重新认证</div><div class="clear"></div></div></li>'
      								      $('.CM-list').append(list)
      								    }
      								    $.pullToRefreshDone('.pull-to-refresh-content');
      							          }
      							        //汽车编辑
      							       $('.car-edit').click(function(){
//    								     		if($(this).closest('li').attr('data-state')==3){
//    								     			sessionStorage.usercar_id_parkCheckCar=$(this).closest('li').attr('name')
//    								     			console.log(sessionStorage.usercar_id_parkCheckCar)
//    								     			$.router.load(parkCheckCarUrl,true);
//    								     		}else{

      								     			 sessionStorage.car_id=$(this).closest('li').attr('name')
                                                     $.router.load(carEditUrl,true);

//    								     		}

      							        })
      							        $('.carManger-list-btn-edit').click(function(){
      							        	sessionStorage.car_carid=$(this).closest('li').attr('name')
      							        	 $.router.load(RecheckCarUrl,true);
      							        })
      			                    },
      		                 });
                     });//下拉刷新end
});